package org.raymond.iworks.study.basic.designpattern.command;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
public class RemoteController {
    private Command[] onCommands;
    private Command[] offCommands;
    private Command undoCommand;
    public RemoteController(){
        onCommands = new Command[5];
        offCommands = new Command[5];
        for(int i=0; i<5; i++){
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }

    // 为按钮设置命令
    public void setCommand(int no, Command onCommand, Command offCommand){
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }
    public void onButtonPushed(int no){
        // 找到按下on的按钮,并调用对应的方法
        onCommands[no].execute();
        // 记录这次的操作,用于准备撤销
        undoCommand = onCommands[no];
    }

    public void offButtonPushed(int no){
        // 找到按下off的按钮,并调用对应的方法
        offCommands[no].execute();
        // 记录这次的操作,用于准备撤销
        undoCommand = offCommands[no];
    }
    public void undoButtonPushed(){
        undoCommand.undo();
    }
}
